package com.alan.wifishare.view.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: ALBaseAdapter
 * @describtion: ALBaseAdapter
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/25 12:31
 */
public abstract class ALBaseAdapter<T> extends BaseAdapter {



    protected Context context;
    protected LayoutInflater mInflater;
    protected List<T> itemList = new ArrayList<T>();

    public ALBaseAdapter(Context context) {
        super();
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }
    /**
     * 设置数据，此操作会清空之前的数据,在原来的基础上添加新的数据可调用{@link #addItems}
     * @param itemList
     */
    public void setItemList(List<T> itemList) {
        if (itemList != null) {
            this.itemList.clear();
            this.itemList.addAll(itemList);
            notifyDataSetChanged();
        }
    }

    public List<T> getItemList() {
        return itemList;
    }

    /**
     * 在原有 的基础上添加数据
     * @param itemList
     */
    public void addItems(List<T> itemList) {
        if (itemList != null) {
            this.itemList.addAll(itemList);
            notifyDataSetChanged();
        }
    }

    /**
     * 清空数据
     */
    public void clearItems() {
        this.itemList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return itemList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup parentView);


}
